﻿@model ReportByUserListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = "维修人员统计";
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("ReportUser list");
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            维修人员统计
        </h1>
        <div class="pull-right">
            <button class="btn btn-success" type="submit" name="exportbyuserexcel-all">
                <i class="fa fa-file-excel-o"></i>
                @T("Admin.Common.ExportToExcel.All")
            </button>
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-2 col-search">
                                @Html.NopLabelFor(model => model.SearchMonth)
                                <div class="col-content">
                                    @Html.NopEditorFor(model => model.SearchMonth)
                                </div>
                            </div>
                            <div class="col-md-2 col-search">
                                @Html.NopLabelFor(model => model.SearchDepartId)
                                <div class="col-content">
                                    @Html.EditorFor(model => model.SearchDepartId, new { SelectList = Model.AvailableDepartmentMenus })
                                </div>
                            </div>
                            <div class="col-md-2 col-search">
                                @Html.NopLabelFor(model => model.SearchUserId)
                                <div class="col-content">
                                    @Html.NopDropDownListFor(model => model.SearchUserId, Model.AvailableUsers)
                                </div>
                            </div>
                            <div class="col-md-2">
                                <button type="button" id="search-customers" class="btn btn-primary"><i class="fa fa-search"></i>@T("Admin.Common.Search")</button>
                                <button type="button" id="cancel-customers" class="btn btn-default"><i class="fa fa-refresh"></i>重置</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="customers-grid"></div>

                        <script type="text/javascript">
                        $(document).ready(function() {
                            $("#customers-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("ReportByUsers", "Maintenance"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    requestEnd: function (e) {
                                        if (e.type == "read") {
                                            var response = e.response;
                                            if (response) {
                                                //store extra data
                                                reportU = e.response["ExtraData"];
                                            }
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)],
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                dataBound: onDataBoundU,
                                columns: [{
                                    field: "TrueName",
                                    title: "姓名",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                }, {
                                    field: "DepartmentName",
                                    title: "部门",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                },  {
                                    field: "TeamName",
                                    title: "班组",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                }, {
                                    field: "HoursString",
                                    title: "共计工时",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:right" },
                                    footerTemplate: '<div style="text-align:right;"><span id=\"u-total-hour\"></span></div>'
                                },  {
                                    field: "Amount",
                                    title: "共计次数",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:right" },
                                    footerTemplate: '<div style="text-align:right;"><span id=\"u-total-amount\"></span></div>'
                                },{
                                    field: "Per",
                                    title: "平均工时",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:right" },
                                },{
                                    field: "MonthHours",
                                    title: "月度总工时",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:right" },
                                },{
                                    field: "Efficiency",
                                    title: "月度工作效率",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:right" },
                                }]
                            });
                                //search button
                            $('#search-customers').click(function() {
                                var grid = $('#customers-grid').data('kendoGrid');
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                selectedIds = [];
                                return false;
                            });
                            //cancel button
                            $('#cancel-customers').click(function () {
                                //cancel
                                var grid = $('#customers-grid').data('kendoGrid');
                                $('#@Html.FieldIdFor(model => model.SearchMonth)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchDepartId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchUserId)').val('0');
                                $("#select_menuSearchDepartId").attr("value", "全部");
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });
                            $("#@Html.FieldIdFor(model => model.SearchDepartId)").change(function () {
                                var selectedItem = $(this).val();
                                var ddlStates = $("#@Html.FieldIdFor(model => model.SearchUserId)");
                                $.ajax({
                                    cache: false,
                                    type: "GET",
                                    url: "@(Url.Action("GetUsersByDepartmentId", "Customer"))",
                                    data: { "departId": selectedItem, onlyRepairman: true },
                                    success: function(data) {
                                        ddlStates.html('');
                                        $.each(data, function(id, option) {
                                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                        });
                                    },
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        alert('Failed to retrieve states.');
                                    }
                                });
                            });
                        });

                        function additionalData() {
                        var data = {
                                SearchMonth: $('#@Html.FieldIdFor(model => model.SearchMonth)').val(),
                                SearchDepartId: $('#@Html.FieldIdFor(model => model.SearchDepartId)').val(),
                                SearchUserId: $('#@Html.FieldIdFor(model => model.SearchUserId)').val(),
                                    };
                                    addAntiForgeryToken(data);
                                    return data;
                        }

                        function onDataBoundU(e) {
                            //update order totals summary
                            if (reportU != "") {
                            $('#u-total-amount').text(reportU['Amount']);
                            $('#u-total-hour').text(reportU['HoursString']);
                            }
                        }
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
}
